/**
 * @copyright 2018 sinping.com 北京芯平科技股份有限公司. All rights reserved.
 * 本内容仅限于北京芯平科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */

package com.sinping.qh.utils.worddocx;


import freemarker.template.Configuration;
import freemarker.template.Template;
import org.apache.poi.util.IOUtils;
import org.docx4j.Docx4J;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * word
 *
 * @author 刘庆沛
 * @create 2018/8/20
 */
public class WordTools {
    private final static Logger _logger = LoggerFactory.getLogger(WordTools.class);
    private static Configuration configuration = null;

    public static void main(String[] args) throws Docx4JException, FileNotFoundException {
//        Map<String, Object> dataMap = new HashMap<>();
//        // dataMap.put("tempdateName", "测试");
//
//        File resumeFile = createDoc("D:\\word\\test.xml", dataMap, "2013.ftl");
//
//        WordprocessingMLPackage wmlPackage = (WordprocessingMLPackage) WordprocessingMLPackage.load(new FileInputStream(resumeFile));
//        File file = new File("D:\\word\\test.docx");
//        wmlPackage.save(file, Docx4J.FLAG_SAVE_ZIP_FILE);
    }

    public static Configuration getConfiguration() {
        configuration = new Configuration(Configuration.VERSION_2_3_23);
        try {
            //模板存放的路径
            URL resourcePath = WordTools.class.getClassLoader().getResource("template");
            File file = new File(resourcePath.getPath());
            //configuration.setDirectoryForTemplateLoading(new File("D:\\word"));
            configuration.setDirectoryForTemplateLoading(file);
        } catch (IOException e) {
            _logger.error(e.getMessage(), e);
        }
        configuration.setDefaultEncoding("utf-8");
        configuration.setLogTemplateExceptions(false);
        return configuration;
    }

    public static File createDoc(String fileName,
                                 Map<?, ?> dataMap, String name) {
        configuration = getConfiguration();
        Writer w = null;
        File f = new File(fileName);
        FileOutputStream fos = null;
        try {
            Template t = configuration.getTemplate(name);
            fos = new FileOutputStream(f);
            w = new OutputStreamWriter(fos, "utf-8");
            t.process(dataMap, w);
            w.close();
        } catch (Exception ex) {
            //TODO
        } finally {
            IOUtils.closeQuietly(w);
            IOUtils.closeQuietly(fos);
        }
        return f;
    }

    /**
     * 生成2013word docx 依赖包
     * 李明星发的，暂时没有用，现在用的是<!--freemarker生成的2013 word文档 start-->之间的依赖包
     */
        //<dependency>
        //<groupId>org.docx4j</groupId>
        //<artifactId>docx4j-MOXy-JAXBContext</artifactId>
        //<version>3.0.0</version>
        //</dependency>
        //
        //<dependency>
        //<groupId>org.eclipse.persistence</groupId>
        //<artifactId>org.eclipse.persistence.moxy</artifactId>
        //<version>2.5.1</version>
        //</dependency>
        //
        //<dependency>
        //<groupId>org.freemarker</groupId>
        //<artifactId>freemarker</artifactId>
        //<version>2.3.23</version>
        //</dependency>
        //
        //<dependency>
        //<groupId>org.docx4j</groupId>
        //<artifactId>docx4j</artifactId>
        //<version>3.2.1</version>
        //</dependency>
}
